package Servlet;

import Dao.doctorDaoImpl;
import Dao.outpatientDao;
import Dao.scheduleDao;
import bean.Doctor;
import bean.OutpatientAppointment;
import bean.ReservationObject;
import bean.doctor_schedule;
import utils.Age;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/ChooseDoctorServlet")
public class ChooseDoctorServlet extends BaseServlet{
    doctorDaoImpl dao = new doctorDaoImpl();
    scheduleDao scheduleDao = new scheduleDao();
    protected void GetDoctors(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String name = req.getParameter("name");
        String phonenumber = req.getParameter("phonenumber");
        String idNumber = req.getParameter("IdNumber");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        int age = Age.AgeOfAcquisition(birthday);
        HttpSession session = req.getSession();//保存在session域中
        String emergency = req.getParameter("emergency");//急门诊判断
        session.setAttribute("emergency",emergency);
        ReservationObject object = new ReservationObject(name,phonenumber,idNumber,sex,birthday,age);//将患者出诊信息封装为一个对象
        session.setAttribute("patient_information",object);
        //获取预约科室
        String value  =  req.getParameter("department");
        String department = null;
        switch (value) {
            case "1":
                // 当expression的值等于value1时执行
                //statements;
                department = "内科";
                break;
            case "2":
                department = "外科";
                break;
            case "3":
                department = "妇产科";
                break;
            case "4":
                department = "儿科";
                break;
            case "5":
                department = "眼科";
                break;
            case "6":
                department = "口腔科";
                break;
            case "7":
                department = "皮肤科";
                break;
            case "8":
                department = "传染病科";
                break;
            case "9":
                department = "精神科";
                break;
            case "10":
                department = "影像科";
                break;
            case "11":
                department = "麻醉科";
                break;
        }
        req.setAttribute("office",department);
        doctorDaoImpl doctorDao = new doctorDaoImpl();
        List<Doctor> doctorList = doctorDao.queryDoctorsByDepartment(department);
        req.setAttribute("doctorList",doctorList);
        req.getRequestDispatcher("/ChooseDoctor.jsp").forward(req,resp);
    }


    protected void appointment_doctor(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String dname = req.getParameter("dname");
        Doctor doctor = dao.queryDoctorByName(dname);//通过医生名字获取医生对象
        req.setAttribute("appointed_doctor",doctor);
        String week = Age.getWeek();
        doctor_schedule doctorSchedule = scheduleDao.queryScheduleByNameAndWeek(dname, week);//获取当天的排班信息
        String status = scheduleDao.queryStatusByNameAndWeek(dname, week);//获取此医生当天的预约状态
        req.setAttribute("status",status);
        req.setAttribute("doctorSchedule",doctorSchedule);
        req.getRequestDispatcher("/appointment_doctor.jsp").forward(req,resp);
    }


    protected void insertRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String dname = req.getParameter("dname");
        Doctor doctor = dao.queryDoctorByName(dname);
        HttpSession session = req.getSession();
        ReservationObject patient = (ReservationObject) session.getAttribute("patient_information");
        String emergency = (String) session.getAttribute("emergency");
        String default_status = "排队中";
        String Sign_in_situation = "未签到";
        OutpatientAppointment record = new OutpatientAppointment(null,Age.getDateAndTime()[0],Age.getDateAndTime()[1],patient.getName(),patient.getSex(),String.valueOf(patient.getAge()),doctor.getDname(),default_status,null,patient.getIdNumber(),emergency,Sign_in_situation,null,doctor);
        outpatientDao  outpatientDao = new outpatientDao();
        outpatientDao.addRecord(record);
        resp.sendRedirect("ReservationSuccessPage.jsp");
    }
}
